import { UploadFile } from 'ant-design-vue';
import type { RuleObject } from 'ant-design-vue/es/form';

export function depreciationMonthRuleFactory(max: number = 240, min: number = 1): RuleObject {
  return {
    validator(_: any, value: any) {
      if (isNaN(value)) {
        return Promise.reject('请填写正确的数字');
      }
      // 判断是否是小数
      if (!/^(\+)?\d+$/.test(value)) {
        return Promise.reject('请填写正整数');
      }
      if (min <= value && value <= max) {
        return Promise.resolve();
      } else {
        return Promise.reject(`该项不能为空,数值范围在${min}-${max}`);
      }
    },
    trigger: ['blur', 'change'],
  };
}
/**
 *
 * @todo
 * 数字范围校验
 * @param range
 *
 * (0,1)
 */
// export function numberRangeRuleFactory(range: string) {
//   return {
//     validator(_: any, value: any) {
//       if (isNaN(value)) {
//         return Promise.reject('请填写正确的数字');
//       }
//       const str = value.toString();
//       // 判断是否是小数
//       if (onlyInt && Number(str) === parseInt(str)) {
//         return Promise.reject('请填写整数');
//       }
//       if (min <= value && value <= max) {
//         return Promise.resolve();
//       } else {
//         return Promise.reject(`该项不能为空,数值范围在${min}-${max}`);
//       }
//     },
//   };
// }

export const circleDayValidator = (rule: RuleObject, value: number) => {
  if (value > 31 || value < 2) {
    return Promise.reject('请输入2-31的整数');
  }
  return Promise.resolve();
};

export function circleRuleValidator(rule: RuleObject, value: object) {
  if (!value) {
    return Promise.reject('至少要有一个工作班次');
  }
  const length = Object.values(value).reduce((prev, next) => prev + next, 0);
  if (length <= 0) {
    return Promise.reject('至少要有一个工作班次');
  }
  return Promise.resolve();
}

export const entUploadFilesRules: RuleObject = {
  validator(rule: RuleObject, value: UploadFile[]) {
    if (Array.isArray(value)) {
      if (value.find(item => item.status === 'uploading')) {
        return Promise.reject('正在上传文件，请耐心等待');
      }
    }
    return Promise.resolve();
  },
};

export const techAreaRules: RuleObject = {
  validator(rule: RuleObject, value: any[]) {
    if (Array.isArray(value) && value.length === 3) {
      return Promise.resolve();
    }
    return Promise.reject('请选择技术领域');
  },
  trigger: ['change', 'blur'],
};
